package com.jerry.constellation.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.jerry.constellation.Adapter.LuckyBaseAdapter;
import com.jerry.constellation.R;
import com.jerry.constellation.Utils.AssetsUtils;
import com.jerry.constellation.bean.StarInfoBean;

import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class Me_Fragment extends Fragment implements View.OnClickListener {

    private CircleImageView meFrg_icno;
    private TextView meFrg_tv;
    private LinearLayout meFrg_about;
    private LinearLayout meFrg_func;
    private LinearLayout meFrg_version;
    private LinearLayout meFrg_updata;
    private Map<String, Bitmap> imageMap;
    private List<StarInfoBean.StarinfoBean> mdata;
    private SharedPreferences sharedPreferences;//存取已选择的头像内容
    int selectpos=0;
    private AlertDialog alertDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        StarInfoBean infoBean = (StarInfoBean) bundle.getSerializable("info");
        mdata = infoBean.getStarinfo();
        sharedPreferences = getContext().getSharedPreferences("star_pref", Context.MODE_PRIVATE);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.me_fragment, container, false);
        initView(view);

        return view;
    }

    private void initView(View view) {
        meFrg_icno = view.findViewById(R.id.meFrg_icno);
        meFrg_tv = view.findViewById(R.id.meFrg_Tv);
        meFrg_about = view.findViewById(R.id.meFrg_about);
        meFrg_func = view.findViewById(R.id.meFrg_function);
        meFrg_version = view.findViewById(R.id.meFrg_version);
        meFrg_updata = view.findViewById(R.id.meFrg_updata);
        alertDialog = new AlertDialog.Builder(getContext()).create();


        //初始化
        imageMap = AssetsUtils.getContentlogimgMap();
        //读取共享参数中保存的星座名称和logo名称
        String defultname = sharedPreferences.getString("name", "白羊座");
        String defultlogoname = sharedPreferences.getString("logoname", "baiyang");

        Bitmap bitmap = imageMap.get(defultlogoname);
        meFrg_icno.setImageBitmap(bitmap);
        meFrg_tv.setText(defultname);
        //设置点击事件
        meFrg_icno.setOnClickListener(this);
        //事件监听
        meFrg_about.setOnClickListener(this);
        meFrg_func.setOnClickListener(this);
        meFrg_version.setOnClickListener(this);
        meFrg_updata.setOnClickListener(this);


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.meFrg_icno:
                showDialig();
                break;
            case R.id.meFrg_about:
                alertDialog.setIcon(R.drawable.tips);//图标
                alertDialog.setTitle("关于我们");//标题
                alertDialog.setMessage("**********==********");//内容
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        // Toast.makeText(Login.this,"您单击了“是”按钮",Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog.show();//显示对话框
                break;
            case R.id.meFrg_function:
                //设置属性
                alertDialog.setIcon(R.drawable.tips);//图标
                alertDialog.setTitle("功能介绍");//标题
                alertDialog.setMessage("Constellation\n" +
                        "是一款关于星座详情，星座配对,星座运势等内容的查询与展示的软件");//内容
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        // Toast.makeText(Login.this,"您单击了“是”按钮",Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog.show();//显示对话框
                break;
            case R.id.meFrg_version:

//                //设置属性
                alertDialog.setIcon(R.drawable.tips);//图标
                alertDialog.setTitle("版本信息");//标题
                alertDialog.setMessage("version 1.0.0");//内容
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        // Toast.makeText(Login.this,"您单击了“是”按钮",Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog.show();//显示对话框
                break;
            case R.id.meFrg_updata:
                alertDialog.setIcon(R.drawable.tips);//图标
                alertDialog.setTitle("软件更新");//标题
                alertDialog.setMessage("暂无可用的软件版本更新");//内容
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        // Toast.makeText(Login.this,"您单击了“是”按钮",Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog.show();//显示对话框
                break;
            default:

                break;
        }
    }

    private void showDialig() {
        final Dialog dialog = new Dialog(getContext());
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.mefrg_dialog_layout, null);

        dialog.setContentView(dialogView);
        dialog.setTitle("请选择您的星座");
        GridView dialog_Gv = dialog.findViewById(R.id.mefrg_dialog_Gv);
        //设置适配器
        LuckyBaseAdapter l = new LuckyBaseAdapter(getContext(), mdata);
        dialog_Gv.setAdapter(l);
        //设置dialog可以被取消
        dialog.setCancelable(true);
        //设置点击外部的控件可以取消dialog
        dialog.setCanceledOnTouchOutside(true);

        //设置监听
        dialog_Gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                StarInfoBean.StarinfoBean bean = mdata.get(i);
                String name = bean.getName();
                String logoname = bean.getLogoname();
                meFrg_tv.setText(name);
                Bitmap bitmap = imageMap.get(logoname);
                meFrg_icno.setImageBitmap(bitmap);
                selectpos=i;//保存所选择的位置
                dialog.cancel();
            }
        });
        dialog.show();
    }

    @Override
    public void onPause() {
        super.onPause();
        StarInfoBean.StarinfoBean bean = mdata.get(selectpos);
        String name = bean.getName();
        String logoname = bean.getLogoname();
        SharedPreferences.Editor edit = sharedPreferences.edit();//获取写入对象
        edit.putString("name",name);
        edit.putString("logoname",logoname);
        edit.commit();
    }
}
